package com.android.segito;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver
{
	NotificationManager 		_nm;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		Log.d("AlarmReceiver", "Alarm Recieved");
		// Get notification manager system service.
		_nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Log.d("AlarmReceiver", "got notification service");

		Task t = (Task) intent.getSerializableExtra("task");
		String startEnd = intent.getStringExtra("start/end");
		String message = null;
		CharSequence from =  t.getTitle();

		if (startEnd.equals("start"))
			message = "\"" + t.getTitle() + "\" has started";
		else
			message = "Have you finished \"" + t.getTitle() + "\"?";

		// Build a new intent for the popup activity, exactly the same, different class.
		Intent notiIntent = new Intent(context, PopUp.class)
				.putExtra("task", t)
				.putExtra("start/end", startEnd)
				.setAction(intent.getAction())
				.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
																notiIntent,
																PendingIntent.FLAG_CANCEL_CURRENT);
		Log.d("AlarmReceiver", "set pending intent");

		// Build a notification.
		Notification noti = new Notification.Builder(context)
				.setSmallIcon(R.drawable.icon)
				.setContentTitle(from)
				.setContentText(message)
				.setContentIntent(contentIntent)
				.build();

		Log.d("AlarmReceiver", "set notification");
		noti.flags = Notification.FLAG_AUTO_CANCEL;
		_nm.notify(t.hashCode(), noti);
		Log.d("AlarmReceiver", "notified");  
	}

}